﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace JeanFish.Common
{
    public static class ConfigurationHelper
    {
        public static string GetConfigurationPropertyName<TConfig, T>(this TConfig config, Expression<Func<TConfig, T>> selector)
            where TConfig : ConfigurationElement
        {
            MemberExpression memberExp = selector.Body as MemberExpression;
            string propertyName = memberExp.Member.Name;
            ConfigurationPropertyAttribute attribute = config.GetPropertyAttribute<ConfigurationPropertyAttribute>(propertyName, null);
            if (attribute == null)
                throw new NotSupportedException();
            return attribute.Name;
        }
    }
}
